/*
 Copyright (c) 2007, Malcolm Sparks
 moxl - Modular XML Library

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package moxl.xpath.parser.states.step;

import moxl.tokenizer.Token;
import moxl.tokenizer.TokenStream;
import moxl.xpath.axes.XPathAxis;
import moxl.xpath.parser.XPathParser;
import moxl.xpath.parser.XPathParserException;
import moxl.xpath.parser.states.XPathParserState;
import moxl.xpath.parser.types.NameMatcher;
import moxl.xpath.parser.types.NonTerminal;
import moxl.xpath.parser.types.Step;
import moxl.xpath.tokenizer.XPathTokenTypes;

public class XPathStepStartState extends XPathParserState
{
	private static XPathParserState instance = new XPathStepStartState();

	public static XPathParserState getInstance()
	{
		return instance;
	}

	@Override
	public void acceptAxisName(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		Step step = (Step) nonTerminal;
		try
		{
			step.setAxis(XPathAxis.selectAxisByName(token.getImage()));
		}
		catch (IllegalArgumentException e)
		{
			throw new XPathParserException("Unidentified axis name (" + token.getImage() + ") specified at " + token.getLocation(), e);
		}

		parser.changeState(XPathStepExpectingDoubleColonState.getInstance());
	}

	@Override
	public void acceptAtSymbol(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		Step step = (Step) nonTerminal;
		step.setAxis(XPathAxis.ATTRIBUTE);
		parser.changeState(XPathStepExpectingNodeTestState.getInstance());
	}

	/**
	 * This indicates an implied child::
	 */
	@Override
	public void acceptNameTest(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal) throws XPathParserException
	{
		Step step = (Step) nonTerminal;
		if (step.getAxis() == null)
		{
			step.setAxis(XPathAxis.CHILD);
		}
		step.setNodeTestMatcher(new NameMatcher(step.getAxis(), token.getImage()));
		if (lookahead(tstream, 1) != null && lookahead(tstream, 1).getType().equals(XPathTokenTypes.OPEN_BRACKET))
		{
			parser.changeState(XPathStepExpectingOpenPredicateState.getInstance());
		}
		else
		{
			parser.changeState(XPathStepEndState.getInstance());
		}
	}

	/**
	 * This indicates an implied child::
	 */
	@Override
	public void acceptNodeType(XPathParser parser, TokenStream tstream, Token token, NonTerminal nonTerminal)
	{
		Step step = (Step) nonTerminal;
		if (step.getAxis() == null)
		{
			step.setAxis(XPathAxis.CHILD);
		}
		step.setNodeTestMatcher(new NodeTypeMatcher(token.getImage()));
		parser.changeState(XPathStepNodeTypeExpectingOpenParenthesisState.getInstance());
	}

}
